<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客草稿页</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
    <script src="js/afterLogin.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/default.jpg" alt="">
        <a class="title" href="blog_list.html">博客系统</a>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="javascript:myIndex()">我的主页</a>
        <a href="myinfo.html">个人资料</a>
        <a href="blog_add.html">写博客</a>
        <a href="javascript:mydraft()">我的草稿</a>
        <a href="private_letter.html">好友</a>
        <a href="javascript:logout()">注销</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="" id="photo" class="avtar" alt="">
                <h3 id="username"></h3>
                <a href="#" id="gitee" target="_blank">github 地址</a>
                <div class="counter">
                    <img src="img/writeLogo.png" >
                    <div>创作 <span id="artCount"></span> 篇文章</div>
                </div>
                <div class="counter">
                    <img src="img/likesLogo.png" alt="">
                    <div>获得 <span id="likesCount"></span> 次点赞</div>
                </div>
                <div class="counter">
                    <img src="img/commentLogo.png" alt="">
                    <div>内容获得 <span id="commentsCount"></span> 次评论</div>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div id="listInfo" class="container-right">

        </div>
    </div>

    <script>
        //获取 url 中的 id 判断是哪个用户的个人博客
        var autId = getUrlValue("id");


        //获取当前登录用户信息
        function getCurUserInfo() {
            var username = jQuery('#username');
            var artCount = jQuery('#artCount');
            var likes = jQuery('#likesCount');
            var comments = jQuery('#commentsCount');
            var title = jQuery('#mytitle');
            var followDiv = jQuery('#followDiv');
            jQuery.ajax({
                type: 'GET',
                url: '/user/showinfo',
                data: {
                    "id": autId
                },
                success: function (result) {
                    if (result != null && result.code == 200 && result.data != null) {
                        //用户信息获取成功
                        jQuery("#photo").attr("src", result.data.photo);
                        //检测是否有 gitee 地址
                        var gitee = jQuery("#gitee");
                        if(result.data.gitee == null || result.data.gitee == "") {
                            //不存在 gitee 地址
                            gitee.text("暂无 gitee 地址");
                        } else {
                            //存在
                            gitee.attr("href", result.data.gitee);
                            gitee.text("gitee 地址");
                        }
                        //用户基本信息
                        username.text(result.data.nickname);
                        title.text(result.data.nickname + "的主页");
                        artCount.text(result.data.artCount);
                        likes.text(result.data.likes);
                        comments.text(result.data.comments);
                    } else {
                        alert("用户信息获取失败");
                    }
                }
            });
        }
        getCurUserInfo();

        //获取草稿列表
        function getMyDraftList() {
            jQuery.ajax({
                type: 'POST',
                url: '/art/draft-list',
                data: {},
                success(result) {
                    if (result != null || result.code == 200) {
                        //有两种情况，一种是发表了文章，一种是没有发表文章
                        if (result.data != null && result.data.length > 0) {
                            //用户发表了文章
                            var artListDiv = "";
                            for (var i = 0; i < result.data.length; i++) {
                                //拿到每一篇博客
                                var artItem = result.data[i];
                                artListDiv += '<div class="blog">';
                                artListDiv += '<a href="blog_content.html?id='+artItem.id+'">';
                                artListDiv += '<div class="title">'+artItem.title+'</div>';
                                artListDiv += '<div class="desc">';
                                artListDiv += artItem.content;
                                artListDiv += '</div>';
                                artListDiv += '<div class="other">';
                                artListDiv += '<span id="date">上一次草稿保存时间:'+artItem.updatetime+'</span>';
                                artListDiv += '<img src="img/阅读.png" alt="">';
                                artListDiv += '<span id="read">'+artItem.rcount+'</span>';
                                artListDiv += '<img src="img/likes.png" alt="">';
                                artListDiv += '<span id="likes">'+artItem.likes+'</span>';
                                artListDiv += '<a href="blog_edit.html?id=' + artItem.id + '" id="edit" target="_blank">修改草稿</a>'
                                artListDiv += '<a href="javascript:delArt(' + artItem.id + ')" id="del">删除草稿</a>'
                                artListDiv += '</div>';
                                artListDiv += '</a>';
                                artListDiv += '</div>';
                                artListDiv += '<hr>';
                            }
                            //将 html 填充进去
                            jQuery("#listInfo").html(artListDiv);
                        } else {
                            //用户未发表过文章
                            jQuery("#listInfo").html("<h3>暂无草稿 <h3>");
                        }
                    } else {
                        alert("文章查询出错，请稍后重试!");
                    }
                }
            });
        }
        getMyDraftList();


        //删除文章
        function delArt(id) {
            if(confirm("确定删除该文章?")) {
                jQuery.ajax({
                    type: 'POST',
                    url: '/art/del',
                    data: {"id": id}, //删除文章的根据
                    success: function(result) {
                        if(result != null && result.code == 200 && result.data == 1) {
                            //文章删除成功
                            alert("恭喜你，文章删除成功!");
                            //刷新页面
                            location.href = location.href;
                        } else {
                            alert("文章删除失败，请稍后再试!");
                        }
                    }
                });
            }
        }


    </script>

</body>
</html>